package com.atguigu.chapter05.transform;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * Author: Pepsi
 * Date: 2023/7/27
 * Desc:
 */
public class Flink03_flatmap {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",1000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        env.fromElements(1,2,3,4)
/*                .flatMap(new FlatMapFunction<Integer, Integer>() {
                    @Override
                    public void flatMap(Integer value, Collector<Integer> out) throws Exception {
                        out.collect(value);
                        out.collect(value*value);
                        out.collect(value*value*value);
                    }
                })*/
                // 改写为lambda表达式，有泛型擦除，因为collector里面有泛型
                .flatMap((FlatMapFunction<Integer, Integer>) (value, out) -> {
                    out.collect(value);
                    out.collect(value*value);
                    out.collect(value*value*value);
                })
                .returns(Types.INT)
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
